package server.main.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import server.main.config.redis.RedisConfig;
import server.main.service.AccountService;
import server.main.service.CaptchaService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @auther MingHsZzz
 * @Date 2022/9/7/21/01
 * @description
 */


@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    CaptchaService captchaService;

    @Autowired
    AccountService accountService;

    @GetMapping("/redis")
    public String setredis() {

//      redisTemplate.opsForValue().setIfAbsent("captcha:" + "1234", "test", 10 * 60L, TimeUnit.SECONDS);
//      String name = redisTemplate.opsForValue().get("captcha:1234");
        String name = captchaService.getCaptcha();


//      if(name instanceof String)
//         return (String)name;
        if (name != null)
            return name;

        return "false";
    }


    @GetMapping("/getmenu")
    public String getmenu() {
        Map<String, Object> map = new HashMap<>();

//      map.put("menu",accountService.getMenuJsonString("tom"));
//      accountService.getMenuJsonString2("tom")
        return null;
    }


    @GetMapping("/getauth")
    public String getauth() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
//        System.out.println(auth.isAuthenticated());


//        System.out.println(auth.isAuthenticated());

//        System.out.println(auth.getClass().getName());

        if(auth == null){

            System.out.println("kong");
        }else{
            System.out.println("success");
            System.out.println(auth.isAuthenticated());

        }

        return null;
    }


}
